package com.itheima.pattern.t15_reponsibility.demo2;

import java.util.ArrayList;
import java.util.List;

/**
 * 模拟实现过滤器链FilterChain
 */
public class FilterChain {

    private List<Filter> filters = new ArrayList<>();

    private int index = 0;

    // 链式调用
    public FilterChain addFilter(Filter filter) {
        filters.add(filter);
        return this;
    }

    public void doFilter(Request req, Response res) {
        if (index == filters.size()) {
            return;
        }
        Filter filter = filters.get(index);
        index++;
        filter.doFilter(req, res, this);
    }
}
